<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Administrator
 * Date: 12-8-29
 * Time: 下午1:56
 * To change this template use File | Settings | File Templates.
 */
class movie extends MY_Model
{
    /**
     * 获取电影列表
     *
     * @param string $field
     * @param int $limit
     * @param int $offset
     * @param null $where
     * @param null $order
     * @return mixed
     */
    public function getMovieList($field = '*', $limit = 20, $offset = 0, $where = null, $order = null)
    {
        $this->db->select($field)->from('movie');
        $this->db->where('status', '1');
        $where && $this->db->where($where);
        $order && $this->db->order_by($order);
        $this->db->limit($limit, $offset);

        return $this->db->get()->result_array();
    }

    /**
     * 获取电影数量
     *
     * @param null $where
     * @return mixed
     */
    public function getMovieCount($where = null)
    {
        $this->db->select('*')->from('movie');
        $this->db->where('status', '1');
        $where && $this->db->where($where);

        return $this->db->count_all_results();
    }

    /**
     * 获取电影数量
     *
     * @param $type
     * @return mixed
     */
    public function getMovieCountByType($type)
    {
        $this->db->select('*')->from('category_correspond');
        $this->db->where(array('category_id'=>$type));
        return $this->db->count_all_results();
    }

    /**
     * 获取电影数量
     *
     * @param $type
     * @param int $Limit
     * @param int $offset
     * @return mixed
     */
    public function getMovieIDByType($type, $Limit=10, $offset=0)
    {
        $this->db->select('resource_id')->from('category_correspond');
        $this->db->where(array('category_id'=>$type));
        $this->db->order_by('update_time DESC');
        $Limit && $this->db->limit($Limit, $offset);
        return $this->db->get()->result_array();
    }

    /**
     * 获取电影
     *
     * @param $mId
     * @param int $limit
     * @param int $offset
     * @param string $field
     * @param null $where
     * @param null $order
     * @return mixed
     */
    public function getMovie($mId, $limit = 20, $offset = 0, $field= "*", $where = null, $order = null)
    {
        $this->db->select($field)->from('movie');
        $this->db->where('status', '1');
        $where && $this->db->where($where);
        $order && $this->db->order_by($order);
        $this->db->limit($limit, $offset);

        if(is_array($mId))
        {
            return  $this->db->where_in('id', $mId)->get()->result_array();
        }
        return $this->db->where('id', $mId)->get()->row_array();
    }

    /**
     * 删除资源
     *
     * @param $mId
     * @return bool
     */
    public function deleteResource($mId)
    {
        $mId = intval($mId);

        if (empty ($mId)) return false;

        return $this->db->delete('movie', array('id' => $mId));
    }

    /**
     * 获取电影 -- 通过中文名
     * @param $name
     * @param int $limit
     * @param int $offset
     * @param string $field
     * @param null $where
     * @param null $order
     * @return mixed
     */
    public function getMovieByChinaName($name, $limit = 20, $offset = 0, $field= "*", $where = null, $order = null)
    {
        $this->db->select($field)->from('movie');
        $this->db->where('status', '1');
        $where && $this->db->where($where);
        $order && $this->db->order_by($order);
        $this->db->limit($limit, $offset);

        if(is_array($name))
        {
            return  $this->db->where_in('china_name', $name)->get()->result_array();
        }
        return $this->db->where('china_name', $name)->get()->row_array();
    }

    /**
     * 获取资源下载地址
     *
     * @param $mId
     * @param int $limit
     * @param int $offset
     * @param string $field
     * @param null $where
     * @param null $order
     * @return mixed
     */
    public function getDownLoad($mId, $limit = 20, $offset = 0, $field= "*", $where = null, $order = null)
    {
        $this->db->select($field)->from('download_list');
        $where && $this->db->where($where);
        $order && $this->db->order_by($order);
        $this->db->limit($limit, $offset);

        return  $this->db->where_in('video_id', $mId)->get()->result_array();
    }

    /**
     * @param $id
     * @return mixed
     */
    public function visit($id)
    {
        $id =  max((int)$id, 0);
        $data = array('views' => 'views+1');
        $this->db->where('id', $id);
        $this->db->set($data, '', false)->update('movie');
        return ;
//        list($riqi, $tmp) = explode(' ', $data['create_time']);
//        $data['key'] = md5("{$data['video_id']}-{$data['video_class']}-{$data['browse_ip']}-{$riqi}");
//        $this->db->insert('movie_count', $data, true);
//        return ;
    }

    /**
     * 记录浏览记录
     *
     * @param array $data
     * @return mixed
     */
    public function recordBrowseLog(array $data)
    {
        $info = array(
            'video_id' => $data['video_id'],
            'video_class' => $data['video_class'],
            'browse_ip' => $data['browse_ip'],
            'ref' => $data['ref'],
            'type' => $data['type'],
            'create_time' => date('Y-m-d H:i:s')
        );

        $this->db->insert('movie_count', $info);
        return $this->db->insert_id();
    }

    /**
     * @param $class
     * @param int $limit
     * @param string $field
     * @return mixed
     */
    public function rank($class, $limit = 10, $field = "*")
    {
        $class = max((int)$class, 0);
        return $this->getMovieList($field, $limit, $offset = 0, array("class"=>$class), "views DESC");
    }
}
